package com.iakuil.mvp.mp.service.impl;

import com.iakuil.mvp.mp.common.BaseServiceImpl;
import com.iakuil.mvp.mp.dao.MpUserInfoMapper;
import com.iakuil.mvp.mp.dao.entity.MpUserInfo;
import com.iakuil.mvp.mp.service.MpUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * MpUserInfoServiceImpl
 *
 * @author Kai
 * @date 2022/1/18 17:01
 **/
@Slf4j
@Service
public class MpUserInfoServiceImpl extends BaseServiceImpl<MpUserInfoMapper, MpUserInfo> implements MpUserInfoService {
    @Override
    public MpUserInfo getByOpenid(String openId) {
        MpUserInfo query = new MpUserInfo();
        query.setOpenId(openId);
        return super.getOne(query);
    }

    @Async
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrModify(MpUserInfo entity) {
        return super.saveOrModify(entity);
    }

    @Async
    @Override
    public boolean bindPhoneNumber(String openId, String phoneNumber) {
        MpUserInfo user = getByOpenid(openId);
        if (user == null) {
            return false;
        }

        log.info("bind mobile and user between {} and {}.", openId, phoneNumber);
        user.setPhoneNumber(phoneNumber);
        return modify(user);
    }
}
